<%@page import="cn.edu.nju.ws.sview.views.data.EntityBrowsing"%>
<%@page import="cn.edu.nju.ws.sview.database.DBOperations"%>
<%@page import="cn.edu.nju.ws.sview.wyge.relations.datasets.DatasetBrowsing"%>
<%@page import="cn.edu.nju.ws.sview.wyge.relations.datasets.DatasetItem"%>
<%@page import="cn.edu.nju.ws.sview.wyge.relations.adataset.datasetDocs.DatasetDocs"%>
<%@page import="cn.edu.nju.ws.sview.rdf.URIResource"%>
<%@page import="cn.edu.nju.ws.sview.wyge.servlets.Dataset"%>
<%@page import="cn.edu.nju.ws.sview.views.Group"%>
<%@page
	import="cn.edu.nju.ws.sview.wyge.relations.adataset.comment.AllComments"%>
<%@page
	import="cn.edu.nju.ws.sview.wyge.relations.adataset.VisitDataset"%>
<%@page import="cn.edu.nju.ws.sview.wyge.relations.adataset.LikeDislike"%>
<%@page
	import="cn.edu.nju.ws.sview.wyge.relations.adataset.DocumentDataset"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@page
	import="cn.edu.nju.ws.sview.wyge.relations.adataset.CustomDataset"%>
<%@page
	import="cn.edu.nju.ws.sview.wyge.relations.adataset.comment.DatasetComment"%>
<%@page import="java.net.URLEncoder"%>
<%@page import="cn.edu.nju.ws.sview.wyge.util.Util"%>
<%@page import="cn.edu.nju.ws.sview.cache.*"%>
<%@page import="java.util.Enumeration"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page
	import="java.util.Map.Entry,java.util.ArrayList,java.util.HashSet,java.util.HashMap"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>数据集</title>
<link rel="icon" href="/sview/favicon.ico" />
<link rel="stylesheet" type="text/css" href="/sview/css/layout.css" />
<link rel="stylesheet" type="text/css" href="/sview/css/smoothness/jquery-ui-1.8.16.custom.css" />
<link rel="stylesheet" type="text/css" href="/sview/css/easyui.css" />
<link rel="stylesheet" type="text/css" href="/sview/css/relations/edataset.css" />
<script type="text/javascript" src="/sview/script/jquery.js"></script>
<script type="text/javascript" src="/sview/script/jquery.easyui.min.js"></script>
<script type="text/javascript" src="/sview/script/jquery-ui-1.8.16.custom.min.js"></script>
<script type="text/javascript" src="/sview/script/global.js"></script>
<script type="text/javascript" src="/sview/script/relations/URILocalname.js"></script>
<script type="text/javascript" src="/sview/script/relations/jquery.cookie.js"></script>
<script type="text/javascript" src="/sview/script/relations/edataset.js"></script>
<script type="text/javascript" src="/sview/script/jquery.jstree.js"></script>
<script type="text/javascript" src="/sview/script/relations/ds_entities.js"></script>
<link rel="stylesheet" type="text/css" href="/sview/css/relations/ds_relations.css">
<script type="text/javascript" src="/sview/script/relations/ds_relations.js"></script>
</head>
<body>
	<%
		String lang = request.getParameter("lang");
		if (lang != null && lang.equals("en")) {
			request.getRequestDispatcher("edataset.jsp").forward(
					request, response);
			return;
		} else if (lang == null) {
			lang = "zh";
		}
		String openOp = request.getParameter("openOp");//for tab control   =[entity|relation]
		String ugname = (String)session.getAttribute("groupName");
		int ugid = -1;
		if(ugname != null){
			ugid = Group.getGroupIdByName(ugname);
		}		
		String idStr = request.getParameter("id");
		String genre = null;
		String ds_type = null;
		int datasetId = -1;
		if (idStr == null) {
			String location = "/sview/relations/index.jsp";
			if (lang.equals("zh"))
				location = location + "?lang=" + lang;
			response.sendRedirect(location);
			return;
		} else {
			genre = idStr.substring(0, 1);//
//			System.out.println("idStr:"+idStr);
//			datasetId = Integer.parseInt(idStr.substring(1));
//			ds_type = genre.equals("c") ? "custom" : "uri";
			if(genre.equals("c")){
				ds_type = DatasetItem.CUSTOM_GENRE;
				idStr = idStr.substring(1);
				datasetId = Integer.parseInt(idStr);
			}else if(genre.equals("u")){
				ds_type = DatasetItem.URI_GENRE;
				idStr = idStr.substring(1);
				datasetId = Integer.parseInt(idStr);
			}else if(genre.equals("t")){
				ds_type = DatasetItem.TEMP_GENRE;
				idStr = idStr.substring(1);
			}
		}
		int gid = -1;
		String gname = null;
		String user = (String)session.getAttribute("user");
		//============================
		HashSet<Integer> uris = new HashSet<Integer>();
		if(genre.equals("c") || genre.equals("u")){
			uris = DatasetBrowsing.getMembersByDatasetId(datasetId,ds_type);
		}else{
			String[] list = idStr.split("_");
			for(String muid : list){
				if(muid.trim().length()>0){
					uris.add(Integer.parseInt(muid));
				}
			}
		}
		for(Integer mid :uris){
	    	String status = DBOperations.getSuccessStatus(mid);
			if(!status.equals(URIResource.VALID_SUCCEED)){
				/* start:由于DBpedia等网站会被封掉，因此使用falcons数据集作为后备*/
				try{
			      EntityBrowsing.getReserveRDFDescription(URIIDCache.getInstance().getURI(mid));
				}catch(Throwable e){
					e.printStackTrace();
				}
			    /* end:由于DBpedia等网站会被封掉，因此使用falcons数据集作为后备*/   
			}
	    }  
	    //=====================
	%>
  <jsp:include page="/vheader_zh.jsp" flush="true">
     <jsp:param name="f" value="d"/>
  </jsp:include>
	<div class="container">
		<div class="generalInfo">
			<div class="general_info">
				<div class="general-info-title">
					<%
						String name = DatasetItem.getShowNameByDatasetID(datasetId, ds_type, lang);//null;
						if (genre.equals("c")) {
							//name = CustomDataset.getNameByCustomDatasetID(datasetId);
							gname = (String)CustomDataset.getGroupByCustomDatasetId(datasetId);
							gid = Group.getGroupIdByName(gname);
//						} else if (genre.equals("u")) {
//							try {
								//name = URIIDCache.getInstance().getURI(datasetId);
								//name = URIResource.getLabel(datasetId, lang);
//							} catch (Throwable e) {
//								e.printStackTrace();
//							}
						}
						//VisitDataset.visit(user, ds_type, datasetId, lang);
						//int[] likeDislike = LikeDislike.getLikeDislike(ds_type, datasetId);
					%>
					<div class="titlename relative">
						<span class="entity-label" id="dsname" title="<%=name%>" genre="<%=genre %>" ds_type="<%=ds_type %>" datasetId="<%=datasetId %>" ><%=Util.trimString(name, 50)%></span>
						<%if(user != null){ %>
						<span class="entity_op">
				   			<a id="bookmarkButton" href="javascript:void(0)"  class="easyui-linkbutton bookmark_op" iconCls="icon-bookmark" plain="true" title="Bookmark this dataset">收藏</a>
				   			<%if(ugid>0){ %>
				   			<a id="newDataset" href="javascript:void(0)"  class="easyui-linkbutton custom_op" iconCls="icon-identity" plain="true" title="Customize a dataset">定制</a>
	   						<%} %>
				   		</span>
				   		<%} %>
					<%--<a href="javascript:void(0)" onclick="return false" id="bookmarkButton">
							<span class="button-bg">
								<span class="bookmark">Bookmark</span>
							</span>
						</a> 
						 <a href="javascript:void(0)" style="position: absolute; right: 200px; text-decoration: none;" id="like" name="<%=genre + datasetId%>">
						<img src="/sview/icons/like.png" style="width: 20px; border: 0;" /> 
						<span style="font-size: small; color: #1170A3;"><%=likeDislike[0]%></span>
						</a> 
						<a href="javascript:void(0)" style="position: absolute; right: 130px; text-decoration: none;" id="dislike" name="<%=genre + datasetId%>">
							<img src="/sview/icons/dislike.png" style="width: 20px; border: 0;" /> 
							<span style="font-size: small; color: #1170A3;"><%=likeDislike[1]%></span>
						</a> --%>
					</div>
				</div>
				<div class="mspacingLittle"></div>

				<div class="commentBox">
					<%
						String comment = null;
//						ArrayList<DatasetComment> commentArray = null;
						if(ds_type.contentEquals("uri")){
							comment = DatasetComment.getUriDescription(datasetId, lang);//.getOriginDescription(datasetId, lang);//.getOriginComment(datasetId, lang);
							if(comment != null && comment.length()>0){
						%>
					<span>描述:</span>
					<span class="comment-box">
						<%=comment%>
					</span>
					<%
						}
					}else if(ds_type.contentEquals("custom")){
						comment = DatasetComment.getCustomDescription(datasetId, lang);//.getText();
							//commentArray = DatasetComment.DatasetComment.getCommentsByDatasetLangUserOrderByTime(ds_type, datasetId, lang, null, 0, 1);
								//.getCommentsByDatasetLangUserOrderByScore(ds_type, datasetId, lang, null, 0, 1);
						/* for (DatasetComment dc : commentArray) { */
						//if(commentArray.size()>0){//only show first comment
						//	DatasetComment dc = commentArray.get(0);
						//	comment = dc.getText();
							//SimpleDateFormat df = new SimpleDateFormat("MM/dd, yyyy");
							//String date = df.format(dc.getTime());
							if(comment != null && comment.length()>0){
					%>
					<span>描述:</span>
					<div class="comment-box">
						<span id="comment"><%=comment%></span>
						
					<%-- <span class="commentMeta">——by <b><%=dc.getAuthor()%></b> on <b><%=date%></b></span> --%>
					</div>
					<%--<span class="datasetComment" id="ds_comment_<%=dc.getCommentID()%>" commentID="<%=dc.getCommentID()%>"> 
						 <a href="javascript:void(0)" style="position: relative; left: 530px; text-decoration: none;" id="ds_comment_like_<%=dc.getCommentID()%>">
							<img src="/sview/icons/like.png" style="width: 15px; border: 0;" /> 
							<span style="font-size: xx-small; color: #1170A3;"><%=dc.getLikeTotal()%></span>
						</a> 
						<a href="javascript:void(0)" style="position: relative; left: 550px; text-decoration: none;" id="ds_comment_dislike_<%=dc.getCommentID()%>">
							<img src="/sview/icons/dislike.png" style="width: 15px; border: 0;" /> 
							<span style="font-size: xx-small; color: #1170A3;"><%=dc.getDislikeTotal()%></span>
						</a> 
					</span>--%>
					<%}
							//if(ugid>0 && ugid == gid){%>
							<!-- <a id="editComment" class='commentOp' href="javascript:void(0);">Edit</a> -->
							<%//}//end of if_ugid
					//	}//end of if_size
						//else if(ugid>0 && ugid == gid){%>
					<!-- <span>Description:</span>
					<div class="comment-box">
						<span id="comment"></span>
							<a id="editComment" class='commentOp' href="javascript:void(0);">Add</a>
							</div> -->
						<%//}
						}//end of if_custom
					%>
				</div>
					<div class="statistics">
						<%
						if(genre.contentEquals("u")){
							String uri = "";
							try{
							uri = URIIDCache.getInstance().getURI(datasetId);
							}catch(Throwable t){
							}
							//String aim = "/sview/dataset/u"+datasetId+"?lang="+lang;//"/sview/entity/o"+datasetId;
							String aim = uri;
							%>
						<span id="source">数据源:</span>
						<span id="<%=datasetId %>"><a href="<%=aim %>"><%=uri %></a></span>
						<%}else if(genre.contentEquals("c")){
							int docNum = uris.size();//CustomDataset.getDocNumByCustomDatasetId(datasetId);
							ArrayList<DatasetItem> docs = DatasetDocs.fetchAll(0, 3, lang, datasetId);%>
						<span id="source">数据源 (共<%=docNum %>个RDF文档):</span>
						<%	
							int i=0;
							for(DatasetItem d : docs){
								String uri = d.getUri();////d.getName();
								String aim = "/sview/dataset/u"+d.getId()+"?lang="+lang;//"/sview/entity/o"+d.getId();
							%>
							<span class="docUri" id="<%=d.getId() %>" order=<%=i++ %>><br />&nbsp;&nbsp;&nbsp;&nbsp;<a href="<%=aim %>"><%=uri %></a></span>
						<%-- <span>customized from <%=docNum %> documents</span> --%>
						<%		
							}
							if(docNum>docs.size()){%>
							<a id="urisOp" class='urisOp moreUris' href="javascript:void(0);" docNum=<%=docNum %>>更多>></a>	
						<%	}
						}else if(genre.contentEquals("t")){
							int docNum = uris.size();%>
						<span id="source">数据源 (共<%=docNum %>个RDF文档):</span>
							
						<%
							int i=0;
							for(int docid : uris){
								String uri = "";
								try{
									uri = URIIDCache.getInstance().getURI(docid);
								}catch(Throwable t){
								}
								String aim = uri;
								String display = "";
								if(i>=3) {
									display = " style='display:none' ";
								}
						%>
							<span class="docUri" id="<%=docid %>" order=<%=i++ %> <%=display %>><br />&nbsp;&nbsp;&nbsp;&nbsp;<a href="<%=aim %>"><%=uri %></a></span>
						<%		
							}
							if(docNum>3){%>
							<a id="urisOp" class='urisOp moreUris' href="javascript:void(0);" docNum=<%=docNum %>>更多>></a>
						<%	}
						}
							//String detail_href = "/sview/relations/ds_detail.jsp?ds_id="+datasetId+"&ds_type="+ds_type+"?lang="+lang;
						 %><%-- <a class="all-detail" target="_blank" href="<%=detail_href %>">
							Details >>
						</a> --%>
						<%-- <a class="all-detail" target="_blank" href="/sview/relations/dataset_comments.jsp?ds=<%=genre + datasetId%>">
							See details >>
						</a> --%>
					</div>
					
				<div class="metadata">
					<%
						if (genre.equals("c")) {
					%>
					编辑组:&nbsp;&nbsp;&nbsp;<span><a target="_self" href="/sview/group/gprofile.jsp?gid=<%=gid%>"><%=gname %></a></span>
					&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;更新时间:&nbsp;&nbsp;&nbsp;<span><%=CustomDataset.getUpdateDateByCustomDatasetID(datasetId, "yyyy/MM/dd")%></span><br /> 
					<%-- <a target="_blank" href="/sview/relations/dataset_docs.jsp?id=<%=datasetId%>">All <%=CustomDataset.getDocNumByCustomDatasetId(datasetId)%>
						RDF documents
					</a> --%>
					<%
						}else if(genre.equals("u")){
						int contId = DocumentDataset.getInstance(datasetId).getContributorId();
						if(contId>0){
							try{
								String contributor = URILabelCache.getInstance().getLabel(contId, lang);
								String href = "/sview/entity/o"+contId+"?lang="+lang;
						%>
					贡献者:&nbsp;&nbsp;&nbsp;<span><a target="_self" href="<%=href %>"><%=contributor %></a></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
					<%
								}catch(Throwable t){
									t.printStackTrace();
								}
						}//end of if_contId
					 	String date = DocumentDataset.getInstance(datasetId).getDate();
					 	if(date != null && date.trim().length()>0){
					 %>
					时间:&nbsp;&nbsp;&nbsp;<span><%=date %></span><br /> 
					<%	}
					}
					%>
				</div>
					
					<%-- <div class="commentOp">
					<% if(ds_type.contentEquals("custom")){ %>
						<a class="all-comment" target="_blank" href="/sview/relations/dataset_comments.jsp?ds=<%=genre + datasetId%>">All
							<span id="allCommentsNum"><%=AllComments.getCommentNum(ds_type, datasetId)%></span>
							comments
						</a>
					<%}
							if (user != null) {
								if(ds_type.contentEquals("custom") && CustomDataset.canEdit(datasetId, ugid)){
						%>
								<a href="javascript:void(0)" class="all-comment" id="addComment">Add comment</a>
								<%} %>
							<a href="javascript:void(0)" class="all-comment" id="newDataset">Add to dataset</a>
						<%
							}
						%>
					</div> --%>
			</div>
		</div>
	</div>
	<div class="main">
	<ul>
		<li class="panel">
			<!-- <a id="toCollection" type="button" class="toCollection easyui-linkbutton">Explore selected collection</a> -->
			 <div id="tab-entity" class="tabBar">
				<!-- <a class="collapse" title="Collapse Panel" href="#"> </a> -->
				<a class="expand" title="展开面板" href="#"> </a>
				<span class="tabName">实体描述</span>
			</div>
			<div id="panel-entity" class="content" style="display:none;"><!--  showingPanel -->
				<div id="panel-entity-div">
					<jsp:include page="ds_entities_zh.jsp" flush="true">
						<jsp:param name="ds_type" value="<%=ds_type %>" />
						<jsp:param name="idStr" value="<%=idStr %>" />
						<jsp:param name="ds_id" value="<%=datasetId %>" />
						<jsp:param name="lang" value="<%=lang %>" />
						<jsp:param name="ugid" value="<%=ugid %>" />
					</jsp:include>
				</div>
			</div>
		</li>
		<li class="panel">
			<div id="tab-relation" class="tabBar">
				<a class="expand" title="展开面板" href="#"> </a>
				<span class="tabName">关系</span>
			</div>
			<div id="panel-relation" class="content" style="display:none;">
				<div>
					<jsp:include page="ds_relations_zh.jsp" flush="true">
						<jsp:param name="ds_type" value="<%=ds_type %>" />
						<jsp:param name="idStr" value="<%=idStr %>" />
						<jsp:param name="ds_id" value="<%=datasetId %>" />
						<jsp:param name="user" value="<%=user%>" />
						<jsp:param name="lang" value="<%=lang%>" />
					</jsp:include>
				</div>
			</div>
		</li>
	</ul>
		<!-- <div id="tabs"> -->
			<%-- <div class="tab-header">
				<div id="originalDiv" class="tab original" >
				    <a href="javascript:void(0)" >Original Relations</a>
				</div>
				<%if(user!=null) {%>
				<div id="favoriteDiv" class="tab favorite" >
					<a href="javascript:void(0)" >Favorite Relations</a>
				</div>
				<%} %>
			    <div id="ordinaryDiv" class="tab ordinary" >
				    <a href="javascript:void(0)" >Available Relations</a>
				</div>		
				<%if(user!=null) {%>
				<div id="trashDiv" class="tab trash" >
					<a href="javascript:void(0)" >Trashed Relations</a>
				</div>                 
				<%} %>	 
			</div>
			<!-- <ul>
				<li><a href="#tabs-1">Overview</a></li>
				<li><a href="#tabs-2">Entities</a></li>
				<li><a href="#tabs-3">Relations</a></li>
			</ul> -->
			<div id="tabs-1">
				<div>
					<jsp:include page="ds_overview.jsp" flush="true">
						<jsp:param name="ds_type" value="<%=ds_type%>" />
						<jsp:param name="ds_id" value="<%=datasetId%>" />
					</jsp:include>
				</div>
			</div>
			<div id="tabs-2" style="display: none;">
				<div>
					<jsp:include page="ds_entities.jsp" flush="true">
						<jsp:param name="ds_type" value="<%=ds_type %>" />
						<jsp:param name="ds_id" value="<%=datasetId %>" />
						<jsp:param name="lang" value="<%=lang %>" />
						<jsp:param name="ugid" value="<%=ugid %>" />
					</jsp:include>
				</div>
			</div>
			<div id="tabs-3">
				<div>
					<jsp:include page="ds_relations.jsp" flush="true">
						<jsp:param name="ds_type" value="<%=ds_type %>" />
						<jsp:param name="ds_id" value="<%=datasetId %>" />
						<jsp:param name="user" value="<%=user%>" />
						<jsp:param name="lang" value="<%=lang%>" />
					</jsp:include>
				</div>
			</div> --%>
		<!-- </div> -->
	</div>
	<input type="hidden" id="display-lang" value="zh" />
	<input type="hidden" id="genre" value="<%=genre %>" />
	<input type="hidden" id="dataset" value="<%=genre + datasetId%>" />
	<input type="hidden" id="ds_id" value="<%=datasetId %>" />
	<input type="hidden" id="ds_type" value="<%=ds_type %>" />
	<input type="hidden" id="idStr" value="<%=idStr %>" />
	<input type="hidden" id="user" value="<%=user%>" />
	<input type="hidden" id="openOp" value="<%=openOp %>" />
	<%if(ugname != null){%>
	<input type="hidden" id="ugname" value="<%=ugname %>" />
	<%} %>
	<div id="dialog_target">
		<div id="bookmark_op" class="section">
			<div class="fields">
				<label>收藏名</label> <input class='ftext itext' type="text"
					id="bookmarkName"></input> <span class="error-msg"></span>
			</div>
		</div>
		<div id="confirm" class="section" style="display: none;">
			<p>
				数据集名: "<span class="dname" style="font-weight: bold"></span>"
				已存在.<br /> 请点击“继续”重新输入其他名称，或点击“取消”结束操作.
		</div>
		<!-- <div id="comment_op" class="section">
			<div class="fields">
				<label>评论:</label>
				<textarea class='ftext' cols="30" rows="5" id="comment_textarea"></textarea>
				<span class="error-msg"></span>
			</div>
		</div> -->
		<div id="onloading" class="section">
			<div style="text-align: center">
				<p>&nbsp;</p>
				<p>正在加载数据，请稍候...</p>
			</div>
		</div>
	</div>
<jsp:include page="/vfooter_zh.jsp"></jsp:include>
</body>
</html>